import cv2
import os
import glob

# 定义原始路径和目标路径
src_dir = '../dataChest/train/label/'
dst_dir = '../dataChest/train/label/'

# 创建目标目录（如果不存在）
os.makedirs(dst_dir, exist_ok=True)

# 获取所有PNG图片路径
image_paths = glob.glob(os.path.join(src_dir, '*.png'))

for path in image_paths:
    # 读取并处理图像
    image = cv2.imread(path)
    if image is not None:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        _, binary = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)

        # 构建保存路径
        filename = os.path.basename(path)
        save_path = os.path.join(dst_dir, filename)

        # 保存处理后的图像
        cv2.imwrite(save_path, binary)
    else:
        print(f"图像读取失败: {path}")
